program define mclogit, eclass
version 15.0
syntax varlist(fv) [if] [in] [,			///
		MC(string) FROM(string) ///
		DIFF NOLOG ITERate(int 300) ///
		VCE(passthru) ///
		TECHnique(passthru) ///
		* ///
		]
		
marksample touse
gettoken depvar indeps:varlist

local fvops = "`s(fvops)'" == "true" | _caller() >= 11 
if `fvops' { 
	local vv: di "version " ///
	string(max(11,_caller())) ", missing: " 
	gettoken lhs rest : varlist
	_fv_check_depvar `lhs'
} 

if `fvops' {
	local rmcoll "version 11: _rmcoll"
}
else {
    local rmcoll _rmcoll
}
`rmcoll' `indeps' if `touse', `constan' `coll'
local indeps `r(varlist)'

local vceopt =	`:length local vce'		|	///
		`:length local weight'		|	///
		`:length local cluster'		|	///
		`:length local robust'
if `vceopt' {
	_vce_parse, argopt(CLuster) opt(OIM OPG Robust) old	///
		: [`weight'`exp'], `vce' `robust' `cluster'
	local vce
	if "`r(cluster)'" != "" {
		local clustvar `r(cluster)'
		local vce vce(cluster `r(cluster)')
	}
	else if "`r(robust)'" != "" {
		local vce vce(robust)
	}
	else if "`r(vce)'" != "" {
		local vce vce(`r(vce)')
	}
}

_get_diopts diopts options, `options'
mlopts mlopts, `options' `vce'  `technique'
ml model lf mclogit_lf (`depvar': `depvar' = `indeps') (a0: `mc') (a1: `mc') if `touse', ///
	max init(`from') `diff' `nolog' iterate(`iterate') `mlopts'

tempname b v ll
matrix `b' = e(b)
matrix `v' = e(V)
local converged = e(converged)
local N = e(N)
scalar `ll' = e(ll)
ereturn post `b' `v', esample(`touse')
local df_m = r(df)
ereturn local title "Misclassification Logit"
ereturn scalar ll = `ll'
ereturn scalar N = `N'
ereturn scalar df_m = `df_m'
ereturn scalar converged = `converged'
ereturn local cmd mclogit
ereturn local predict mclogit_p
ereturn local depvar = `depvar'

_prefix_display, `table' `header' `title' `diopts'

end
exit

